package com.example.sprintbootarithmetic.DataStructure.HashTab;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Scanner;

public class Hashtab {
    private  EmpLinkList[] emplist;
    private  int size;

    public Hashtab(int sizes) {
        this.size=sizes;
        emplist = new EmpLinkList[size];
        for (int i=0;i<emplist.length;i++) {
            emplist[i] = new EmpLinkList();
        }
    }

    private void add(Emp emp) {
        //根据员工的id确定应该加入那一条链表
        int index = findval(emp.getId());
        emplist[index].add(emp);
    }

    public int findval(int var) {
        return var % size;
    }

    public void list() {
       for(int i=0;i<emplist.length;i++){
           emplist[i].list(i);
       }
    }

    public void findvarByid(int id) {
        int empLinkbyid = findval(id);
        emplist[empLinkbyid].findEmplById(id);
    }

    public static void main(String[] args) {
        Hashtab empLinkList = new Hashtab(10);
        while (true) {
            System.out.println("请输入如下指令：\n1:add\n2:find\n3:list\n4:退出");
            Scanner scanner = new Scanner(System.in);
            String next = scanner.next();
            switch (next) {
                case "1":
                    System.out.println("请输入编号:");
                    int id = scanner.nextInt();
                    System.out.println("请输入姓名:");
                    String name = scanner.next();
                    Emp emp = new Emp(id, name);
                    empLinkList.add(emp);
                    break;
                case "2":
                    System.out.println("请输入编号：");
                    int bh = scanner.nextInt();
                    empLinkList.findvarByid(bh);
                    break;
                case "3":
                    empLinkList.list();
                    break;
                default:
                    return;
            }
        }
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static
    class Emp {
        public int id;
        public String name;
        public Emp next;

        public Emp(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    @AllArgsConstructor
    @Data
    @NoArgsConstructor
    static class EmpLinkList {

        private Emp head;

        public void add(Emp emp) {
            if (head == null) {
                head = emp;
                return;
            }
            Emp emp1 = head;
            while (true) {
                if (emp1.next == null) {
                    emp1.next = emp;
                    break;
                }
                emp1 = emp1.next;
            }
        }

        public void findEmplById(int id) {
            if (head == null) {
                return;
            }
            Emp curemp = head;
            while (true) {
                if (curemp.id == id) {
                    break;
                }
                if (curemp.next == null) {
                    System.out.println("对不起，该链表中不存在改节点");
                    return;
                }
                curemp = curemp.next;
            }
        }

        public void list(int i) {
            if (head == null) {
                return;
            }
            System.out.println("当前链表编号为"+i);
            Emp curEMp = head;
            while (true) {
                if (curEMp == null) {
                    break;
                }
                System.out.println("emp:编号为%d" + curEMp.getId() + "对象为%s" + curEMp.toString());
                curEMp = curEMp.next;
            }
        }

        public void del(int id) {
            while (true) {
                Emp emp1 = head.next;
                if (emp1.id == id) {
                    Emp emp2 = emp1.next;
                    head.next.next = emp2;
                    break;
                }
                head = head.next;
            }
        }
    }
}